<?php

namespace App\Services;

use App\Models\UserCoupon;
use Illuminate\Support\Facades\Auth;

class PassengerUserCouponService extends BaseService
{


    /**
     * @param $user_id
     * @param $status
     */
    public function getListAll($user_id, $status)
    {

        $select_field = ['id', 'activity_id', 'coupon_id', 'use_time', 'receive_time', 'overdue_time', 'status'];
        $list = UserCoupon::query()
            ->where('user_id', $user_id)
            ->with(['coupon', 'coupon.service_type'])
            ->where('status', $status)
            ->where(function ($query) {
                $query->where('overdue_time', 0)->orWhere('overdue_time', '>', time() - 86400 * 30);
            })
            ->select($select_field)
            ->orderByDesc('receive_time')
            ->get()->toArray();

        if ($status == 1) { //已领卡
            $list_ = [
                'normal' => [],
                'past' => []
            ];
            foreach ($list as $k => $user_coupon) {
                if ($user_coupon['overdue_time'] != 0 && $user_coupon['overdue_time'] <= time()) {
                    $list_['past'][] = $user_coupon;
                    continue;
                }
                $list_['normal'][] = $user_coupon;
            }
            return $list_;
        } else {
            foreach ($list as $key => $val) {
                $list[$key]['overdue_time'] = $val['coupon']['validity_day'] ? ($val['coupon']['validity_day'] * 86400) + time() : 0;

            }
            return $list;
        }


    }


    /**
     * 领取
     * @param object $user_coupon_info
     */
    public function draw(object $user_coupon_info)
    {

        $user_coupon_info->status = 1;
        $user_coupon_info->receive_time = time();
        $user_coupon_info->overdue_time = $user_coupon_info->coupon->validity_day * 86400 ?
            time() + $user_coupon_info->coupon->validity_day * 86400
            : 0;

        return $user_coupon_info->save();

    }

    /**
     * 批量领取
     */
    public function draws($user_coupon_info, $user_id)
    {
        $user_coupon = new UserCoupon;
        foreach ($user_coupon_info as $value) {
            $user_coupon->status = 1;
            $user_coupon->receive_time = time();
            $user_coupon->overdue_time = $value['coupon']['validity_day'] * 86400 ?
                time() + $value['coupon']['validity_day'] * 86400
                : 0;
            UserCoupon::query()->where('id', $value['id'])->update($user_coupon->toArray());
        }
        return true;
    }


    /**
     * 获取订单可用优惠券
     * @param $service_type_id 服务类型
     * @param $cost_price 行程费用
     * @param $book_time 打车时间
     * @return array
     */
    public function getListAllByOrder($service_type_id, $cost_price, $book_time)
    {

        $book_time = strtotime($book_time);
        $times = time(); //当前时间戳
        $list = UserCoupon::query()
            ->where('user_id', Auth::id())
            ->where('status', 1)
            ->where(function ($query) use ($times) {
                $query->where('overdue_time', 0)->orWhere('overdue_time', '>', $times);
            })
            ->with(['coupon', 'coupon.service_type'])
            ->whereHas('coupon', function ($query) use ($cost_price) {
                $query->where('use_full', '<=', $cost_price);
            })
            ->whereHas('coupon.service_type', function ($query) use ($service_type_id) {
                $query->where('id', $service_type_id);
            })
            ->get()->toArray();

        $_list = [];
        foreach ($list as $k => $user_coupon) {
            if ($user_coupon['coupon']['use_time_start'] == '00:00:00' && $user_coupon['coupon']['use_time_end'] == '00:00:00') {
                $_list[] = $user_coupon;
                continue;
            }
            $book_time = strtotime(date('H:i:s', $book_time));
            $start_time = strtotime($user_coupon['coupon']['use_time_start']);
            $end_time = strtotime($user_coupon['coupon']['use_time_end']);
            if ($book_time <= $start_time || $book_time > $end_time) {
                continue;
            }

            $_list[] = $user_coupon;
        }

        return $_list;

    }

}
